package com.travel.service.Impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.mapper.HotelMapper;
import com.travel.pojo.dto.HotelDTO;
import com.travel.pojo.dto.HotelQueryDTO;
import com.travel.pojo.dto.PageDTO;
import com.travel.pojo.entity.Hotel;
import com.travel.pojo.entity.User;
import com.travel.pojo.vo.HotelVO;
import com.travel.service.IHotelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class HotelServiceImpl extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {

    private final HotelMapper hotelMapper;
    @Override
    public PageDTO<HotelVO> pageListHotel(HotelQueryDTO hotelQueryDTO) {
        Page<Hotel> page = Page.of(hotelQueryDTO.getPage(), hotelQueryDTO.getPageSize());
        String location = hotelQueryDTO.getLocation();
        Double rating = hotelQueryDTO.getRating();
        Integer price = hotelQueryDTO.getPrice();
        String name = hotelQueryDTO.getName();
        Page<Hotel> hotelPage = lambdaQuery()
                .like(StringUtils.isNotBlank(name), Hotel::getName, name)
                .like(StringUtils.isNotBlank(location), Hotel::getLocation, location)
                .ge(rating != null, Hotel::getRating, rating)
                .le(price != null, Hotel::getPrice, price)
                .page(page);
        List<HotelVO> collect = hotelPage.getRecords().stream().map(hotel -> {
            HotelVO hotelVO = BeanUtil.copyProperties(hotel, HotelVO.class);
            hotelVO.setImage(hotel.getPhotoUrl());
            return hotelVO;
        }).collect(Collectors.toList());
        PageDTO<HotelVO> hotelVOPageDTO = new PageDTO<>();
        hotelVOPageDTO.setList(collect);
        hotelVOPageDTO.setPages((int)hotelPage.getPages());
        hotelVOPageDTO.setTotal((int)hotelPage.getTotal());
        return hotelVOPageDTO;
    }

    @Override
    public void updateHotel(Long id, HotelDTO hotelDTO) {
        LambdaUpdateWrapper<Hotel> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(id != null,Hotel::getId,id)
                .set(hotelDTO.getName()!=null,Hotel::getName,hotelDTO.getName())
                .set(hotelDTO.getLocation() != null,Hotel::getLocation,hotelDTO.getLocation())
                .set(hotelDTO.getCoordinates() != null, Hotel::getCoordinates, hotelDTO.getCoordinates())
                .set(hotelDTO.getPrice() != null,Hotel::getPrice,hotelDTO.getPrice())
                .set(hotelDTO.getDescription() != null,Hotel::getDescription,hotelDTO.getDescription())
                .set(hotelDTO.getImage() != null,Hotel::getPhotoUrl, hotelDTO.getImage())
                .set(hotelDTO.getRating() != null,Hotel::getRating, hotelDTO.getRating())
                .set(hotelDTO.getRoomInfo() != null,Hotel::getRoomInfo,hotelDTO.getRoomInfo());
        hotelMapper.update(null,wrapper);
    }

}
